package table.models;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class StatisticsTableModel extends AbstractTableModel {
	private final int columnCount;
	private ArrayList < Object [] > rowData;
	private final String [] columnNames;

	public StatisticsTableModel() {
		this.columnCount = 2;
		this.rowData = new ArrayList < Object [] >();
		this.columnNames = new String [] { "...", "Anzahl" };
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		return false;
	}

	@Override
	public int getColumnCount() {
		return this.columnCount;
	}

	@Override
	public int getRowCount() {
		return this.rowData.size();
	}

	@Override
	public String getColumnName(int column) {
		return this.columnNames [column];
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return this.rowData.get(rowIndex) [columnIndex];

	}

	@Override
	public void setValueAt(Object value, int row, int col) {

		while (this.getRowCount() < row) {
			this.addRow();
		}
		this.rowData.get(row) [col] = value.toString();

		this.fireTableCellUpdated(row, col);
	}

	public void addRow() {
		this.rowData.add(new String [this.columnCount]);
		this.fireTableRowsInserted(this.rowData.size() - 1, this.rowData.size() - 1);
	}

	public void addRow(Object [] objects) {
		this.rowData.add(objects);
	}

	public void addData(String label, Integer number) {
		this.addRow(new Object [] { label, number });
	}

	@Override
	public Class getColumnClass(int c) {
		return this.getValueAt(0, c).getClass();
	}

	public Object [] getRowData(int row) {
		return this.rowData.get(row);
	}

	public void removeRow(int row) {
		this.rowData.remove(row);
		this.fireTableDataChanged();
	}

	public void removeData() {
		this.rowData = new ArrayList < Object [] >();
	}

}
